package com.itheima.reggie.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.domain.Setmeal;
import com.itheima.reggie.domain.SetmealDish;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.mapper.SetmealDishMapper;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

//套餐
@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;
    //分页查询
    @Override
    public Page<Setmeal> findByPage(String name, Integer pageNum, Integer pageSize) {
        //根据name模糊查询套餐
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);
        //分页查询的条件
        Page<Setmeal> page = new Page<>(pageNum,pageSize);
        //执行条件的分页查询
         page = setmealMapper.selectPage(page, wrapper);
         //遍历获取套餐
        List<Setmeal> setmealList = page.getRecords();
        if (CollectionUtil.isNotEmpty(setmealList)){
            for (Setmeal setmeal : setmealList) {
                //根据setmeal的category_id查询category
                Category category = categoryMapper.selectById(setmeal.getCategoryId());
                setmeal.setCategoryName(category.getName());
                //根据setmeal的id查询setmeal_dish列表
                LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
                wrapper1.eq(SetmealDish::getSetmealId,setmeal.getId());
                List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper1);
                setmeal.setSetmealDishes(setmealDishList);
                //组装返回数据
            }
        }

        return page;
    }
    //添加
    @Override
    public void save(Setmeal setmeal) {
        //添加setmeal
        setmealMapper.insert(setmeal);
        //添加setmeal_dish
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)){
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }
    //删除
    @Override
    public void delete(Long[] ids) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(Setmeal::getId,ids);
        wrapper.eq(Setmeal::getStatus,1);
        Integer count = setmealMapper.selectCount(wrapper);
        if (count>0){
            throw new CustomException("您要删除的商品还在在售状态,不能删除");
        }
        //删除套餐菜品
        for (Long id : ids) {
            LambdaQueryWrapper<SetmealDish> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SetmealDish::getSetmealId,id);
            setmealDishMapper.delete(wrapper1);
            //删除套餐
            setmealMapper.deleteById(id);
        }

    }
    //停售
    @Override
    public void status(Integer status, Long[] ids) {
        for (Long id : ids) {
            Setmeal setmeal = new Setmeal();
            setmeal.setStatus(status);
            setmeal.setId(id);
            setmealMapper.updateById(setmeal);
        }
    }
    //回显
    @Override
    public Setmeal findById(Long id) {
        //查询套餐
        Setmeal setmeal = setmealMapper.selectById(id);
        //查询套餐菜品
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
        setmeal.setSetmealDishes(setmealDishList);
        return setmeal;
    }
    //更新
    @Override
    public void update(Setmeal setmeal) {
        //根据主键更新套餐信息
        setmealMapper.updateById(setmeal);
        //根据套餐id删除相关菜品
        //delete from setmeal_dish where id=?
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        setmealDishMapper.delete(wrapper);
        //想套餐中保存新的菜品
        List<SetmealDish> setmealDishList = setmeal.getSetmealDishes();
        if (CollectionUtil.isNotEmpty(setmealDishList)){
            for (SetmealDish setmealDish : setmealDishList) {
                setmealDish.setSetmealId(setmeal.getId());
                setmealDishMapper.insert(setmealDish);
            }
        }
    }
    //查询所有套餐
    @Override
    public List<Setmeal> findList(Long categoryId, Integer status) {
        //设置查询条件
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Setmeal::getCategoryId,categoryId);
        wrapper.eq(Setmeal::getStatus,status);
        List<Setmeal> setmealList = setmealMapper.selectList(wrapper);
        return setmealList;
    }
    //查询套餐详情
    @Override
    public List<Dish> getSetmealDish(Long id) {
        //利用一个集合,把套菜菜品封装起来
        List<Dish> dishList=new ArrayList<>();
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishList = setmealDishMapper.selectList(wrapper);
            //遍历菜品获得每个菜品
        for (SetmealDish setmealDish : setmealDishList) {
                //根据id查询菜品
                Dish dish = dishMapper.selectById(setmealDish.getDishId());
                dishList.add(dish);
        }
        return dishList;
    }
}
